<?php
	/* Functions in this file */
	/**************************/

	// DeleteSymbolArrayContents()
	// GetMFCClassName()
	// GetMFCClassSchema()
	// GetPosition()
	// GetSymbolCount()
	// GetText($index)
	// IsValidActiveSymbol($activeSymbol)
	// IsValidPosition($position)
	// IsValidRepeatNumber($repeatNumber)
	// IsValidSymbolIndex($index)
	// IsValidSymbolType($symbolType)
	// RemoveSymbolAtIndex($index)
	// SetPosition($position)
?>
<?php
	class Direction {
		// Constants
		// Default constants
		const DEFAULT_POSITION = 0;
		// Position Constants
		const MIN_POSITION = 0;
		const MAX_POSITION = 255;
		// Symbol Constants
		const MAX_SYMBOLS = 3;
		const NUM_SYMBOL_TYPES = 19;
		// Repeat Number Constants
		const MIN_REPEAT_NUMBER = 0;
		const MAX_REPEAT_NUMBER = 24;
		// Default Constants
		const coda = 0x00000000;
		const doubleCoda = 0x00000001;
		const segno = 0x00000002;
		const segnoSegno = 0x00000003;
		const fine = 0x00000004;
		const daCapo = 0x00000005;
		const dalSegno = 0x00000006;
		const dalSegnoSegno = 0x00000007;
		const toCoda = 0x00000008;
		const toDoubleCoda = 0x00000009;
		const daCapoAlCoda = 0x0000000a;
		const daCapoAlDoubleCoda = 0x0000000b;
		const dalSegnoAlCoda = 0x0000000c;
		const dalSegnoAlDoubleCoda = 0x0000000d;
		const dalSegnoSegnoAlCoda = 0x0000000e;
		const dalSegnoSegnoAlDoubleCoda = 0x0000000f;
		const daCapoAlFine = 0x00000010;
		const dalSegnoAlFine = 0x00000011;
		const dalSegnoSegnoAlFine = 0x00000012;
		const activeNone = 0x00000000;
		const activeDaCapo = 0x00000001;
		const activeDalSegno = 0x00000002;
		const activeDalSegnoSegno = 0x00000003;
		const symbolTypeMask = 0x0000ff00;
		const activeSymbolMask = 0x000000c0;
		const repeatNumberMask = 0x0000003f;
		static private $directionText = array ("Coda", "Double Coda", "Segno", "Segno Segno",
			"Fine", "D.C.", "D.S.", "D.S.S.", "To Coda", "To Dbl. Coda",
			"D.C. al Coda", "D.C. al Dbl. Coda", "D.S. al Coda", "D.S. al Dbl. Coda",
			"D.S.S. al Coda", "D.S.S. al Dbl. Coda", "D.C. al Fine", "D.S. al Fine",
			"D.S.S. al Fine");

		protected $m_position = self::DEFAULT_POSITION;
		protected $m_symbolArray = array();
		// MFC Class Functions
		function GetMFCClassName() {
			return "CDirection";
		}
		function GetMFCClassSchema() {
			return 1;
		}
		// Position Functions
		function IsValidPosition($position) {
			return (($position >= self::MIN_POSITION) && ($position <= self::MAX_POSITION));
		}
		function SetPosition($position) {
			if (!$this->IsValidPosition($position)) return 0;
			$this->m_position = $position;
			return 1;
		}
		function GetPosition() {
			return ($this->m_position);
		}
		function IsValidSymbolType($symbolType) {
			return ($symbolType <= self::dalSegnoSegnoAlFine);
		}
		function IsValidActiveSymbol($activeSymbol) {
			return ($activeSymbol <= self::activeDalSegnoSegno);
		}
		function IsValidRepeatNumber($repeatNumber) {
			return (($repeatNumber >= self::MIN_REPEAT_NUMBER) && ($repeatNumber <= self::MAX_REPEAT_NUMBER));
		}
		// Symbol Array Functions
		function IsValidSymbolIndex($index) {
			return ($index < $this->GetSymbolCount());
		}
		function GetSymbolCount() {
			return count($this->m_symbolArray);
		}
		// Constructor/Destructor
		function Direction($position=NULL, $symbolType=NULL, $activeSymbol=NULL, $repeatNumber=NULL) {
			//------Last Checked------//
			// - Jan 11, 2005
			if (!$this->IsValidPosition($position)) return 0;
			$this->m_position = $position;
			$this->AddSymbol($symbolType, $activeSymbol, $repeatNumber);
		}
		// Serialize Functions
		
		function DoSerialize(&$stream) {
		
			WRITE_BYTE ($stream,$this->m_position);

			$symbolCount = $this->GetSymbolCount();
			WRITE_BYTE ($stream,$symbolCount);

			$i = 0;
			for (; $i < $symbolCount; $i++) {
				WRITE_WORD ($stream,$this->m_symbolArray[$i]);

			}
			return 1;
		}
		
		
		function DoDeserialize(&$stream) {
		
		
			$this->m_position = READ_BYTE ($stream);
		
			$symbolCount = READ_BYTE ($stream);
		
			$i = 0;
			for ($i = 0; $i < $symbolCount; $i++) {
		
				$symbol = READ_WORD ($stream);
		
				array_push($this->m_symbolArray,$symbol);
			}
	
		}
		// Symbol Functions
		
		function AddSymbol($symbolType, $activeSymbol, $repeatNumber) {
			//------Last Checked------//
			// - Jan 11, 2005
			if (!$this->IsValidSymbolType($symbolType)) return 0;
			if (!$this->IsValidActiveSymbol($activeSymbol)) return 0;
			if (!$this->IsValidRepeatNumber($repeatNumber)) return 0;
			// Can't add anymore $symbols
			if ($this->GetSymbolCount() == self::MAX_SYMBOLS)
				return 0;
			// Add a $symbol to the end of the array, then set the data
			array_push($this->m_symbolArray, 0);
			return ($this->SetSymbol($this->GetSymbolCount() - 1, $symbolType, $activeSymbol, $repeatNumber));
		}
		function SetSymbol($index, $symbolType, $activeSymbol, $repeatNumber) {
			//------Last Checked------//
			// - Jan 11, 2005
			if (!$this->IsValidSymbolIndex($index)) return 0;
			if (!$this->IsValidSymbolType($symbolType)) return 0;
			if (!$this->IsValidActiveSymbol($activeSymbol)) return 0;
			if (!$this->IsValidRepeatNumber($repeatNumber)) return 0;
			$symbol = ($symbolType << 8);
			$symbol |= ($activeSymbol << 6);
			$symbol |= $repeatNumber;
			$this->m_symbolArray[$index] = $symbol;
			return 1;
		}
		function GetSymbol($index, &$symbolType, &$activeSymbol, &$repeatNumber) {
			//------Last Checked------//
			// - Jan 11, 2005
			if (!$this->IsValidSymbolIndex($index)) return 0;
			$symbolType = $activeSymbol = $repeatNumber = 0;
			$symbolType = (($this->m_symbolArray[$index] & self::symbolTypeMask) >> 8);
			$activeSymbol = (($this->m_symbolArray[$index] & self::activeSymbolMask) >> 6);
			$repeatNumber = ($this->m_symbolArray[$index] & self::repeatNumberMask);
			return 1;
		}
		function IsSymbolType($index, $symbolType) {
			//------Last Checked------//
			// - Jan 11, 2005
			if (!$this->IsValidSymbolIndex($index)) return 0;
			if (!$this->IsValidSymbolType($symbolType)) return 0;
			$type = 0;
			$activeSymbol = 0;
			$repeatNumber = 0;
			if (!$this->GetSymbol($index, $type, $activeSymbol, $repeatNumber))
				return 0;
			return ($type == $symbolType);
		}
		function RemoveSymbolAtIndex($index) {
			//------Last Checked------//
			// - Jan 11, 2005
			if (!$this->IsValidSymbolIndex($index)) return 0;
			$this->m_symbolArray->RemoveAt($index);
			return 1;
		}
		function DeleteSymbolArrayContents() {
			//------Last Checked------//
			// - Jan 11, 2005
			$this->m_symbolArray = array();
		}
		function GetText($index) {
			//------Last Checked------//
			// - Jan 11, 2005
			$symbolType = 0;
			$activeSymbol = 0;
			$repeatNumber = 0;
			if (!$this->GetSymbol($index, $symbolType, $activeSymbol, $repeatNumber))
				return "";
			if (!$this->IsValidSymbolType($symbolType)) return "";
			return ($this->directionText[$symbolType]);
		}
	}
?>

